//
//  TTHomeViewController.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/4.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit

class TTHomeViewController: TTBasicTableViewController {
    
    //懒加载上拉加载
    fileprivate lazy var pullUp:UIActivityIndicatorView = {
        
        let pullup = UIActivityIndicatorView(activityIndicatorStyle:UIActivityIndicatorViewStyle.whiteLarge)
        pullup.color = UIColor.red
        
        return pullup
        
    }()
    
    //懒加载下拉加载弹出显示框
    private lazy var alterLabel:UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.white)
        label.text = "没有更多的数据了"
        label.backgroundColor = UIColor.orange
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
    
    fileprivate lazy var pullBussom:TTUIRefreshControl = {
        
        let pull = TTUIRefreshControl()
        
        pull.addTarget(self, action: #selector(loaddata), for: .valueChanged)
        
        return pull
    }()
    
    lazy var ttHomeModel:TTHomeViewModel = TTHomeViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        
     
        if !isfalse
        {
            
            basicView?.setOther(isfa: true, descu: nil, imageName: nil)
            
        }else
        {
            setupUI()
            
            
        }
        
    }
    
    func loaddata()
    {
        
        roloadDate()
        
    }
    
    //设置UI
    func setupUI()
    {
        
        //添加弹框label
        if let nav = self.navigationController{
            
            nav.view.insertSubview(alterLabel, belowSubview: nav.navigationBar)
            alterLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width: nav.navigationBar.frame.size.width, height: 35)
            
        }
        //注册
        tableView.register(TTHomeTableViewCell.self, forCellReuseIdentifier: homeCellIdefier)
        
        //设置cell高度
        
        //  自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        tableView.separatorStyle = .none
        
        //设置pullup
        tableView.tableFooterView = pullUp
        self.tableView.addSubview(pullBussom)
        
        roloadDate()
    }
    
    func roloadDate()
    {
        
        //        self.refreshControl = pullBussom
        //加载数据
        ttHomeModel.setHomeDate(isup: pullUp.isAnimating, isscuees: { (isscuess,mageess) in
            
          
            
            
            //只要不是下拉刷新就不显示
            if self.pullUp.isAnimating == false{
            self.apperdLabel(magess: mageess)
            }
            //关闭
           self.colse()
            
            if isscuess == true
            {
                self.tableView.reloadData()
                
            }
        })
        
    }
    
    // MARK:  --关闭风火轮
    func colse()
    {
        self.pullUp.stopAnimating()
        self.pullBussom.endrerish()
    
    }
    
    // MARK:  --显示弹窗label
    func apperdLabel(magess:String)
    {
        //只要label还在显示就不在开启动画
        if alterLabel.isHidden == false
        {
        
            return
        }
        
        alterLabel.isHidden = false
        alterLabel.text = magess
        UIView.animate(withDuration: 1, animations: {
            self.alterLabel.transform = CGAffineTransform(translationX: 0, y: self.alterLabel.frame.size.height)
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: {
                self.alterLabel.transform = CGAffineTransform.identity
            }, completion: { (_) in
                self.alterLabel.isHidden = true
            })
            
        }
        
    }
    
}




//数据源方法
extension TTHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ttHomeModel.listArray.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: homeCellIdefier, for: indexPath) as! TTHomeTableViewCell
        //去除cell的选择样式
        cell.selectionStyle = .none
        
        cell.satuesModel = self.ttHomeModel.listArray[indexPath.row]
        
        return cell
        
        
    }
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        if indexPath.row == ttHomeModel.listArray.count - 1{
            
            pullUp.startAnimating()
            
            roloadDate()
        }
    }
    
    
}

